/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import gestionnaire.FilmPanier;
import gestionnaire.GestionnaireClient;
import gestionnaire.GestionnairePanier;
import gestionnaire.Gestionnaires;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modeles.Client;
import modeles.Commande;

/**
 *
 * @author MAZID
 */
@WebServlet(name = "ServletSession", urlPatterns = {"/ServletSession"})
public class ServletSession extends HttpServlet {
    @EJB
    private GestionnaireClient gestionnaireClient;
    private GestionnairePanier gestionnairePanier;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String action =request.getParameter("action");
        String page = request.getHeader("Referer");
        if(page==null){page="VenteDeFilm/films";}
        HttpSession session = null;
        String login = request.getParameter("login");
        String pwd = request.getParameter("motDePasse");
        Client client =null;
        FilmPanier panier = null;
        // on test si l'action n'est pas egal a null
        if(action!=null){
            session=request.getSession();
         if(action.equals("testAuthen")){
            //on test si le login et mot de passe est correcte
            client = gestionnaireClient.getClientTestLogin(login, pwd);
            
            if(client!=null){
                
                panier = (FilmPanier)session.getAttribute("panier");
                //Object nb = session.getAttribute("nbFilms");
                if(panier==null){
                    panier = new FilmPanier();
                }
                // Si pas de session, on la détruit et on en crée une nouvelle
                if(!session.isNew())
                {
                    session.invalidate();
                    session=request.getSession();
                }
//                int nb = gestionnairePanier.getNbProduit();
               // request.setAttribute("nbFilms", nb);
                session.setAttribute("connecte", client);  
                session.setAttribute("panier", panier);
                session.setAttribute("nomCL",client.getNomCl());
                session.setAttribute("pnomCl", client.getPnomCl());
                
                //session.setAttribute("connecte", true);
                
                //on rajoute le panier à la session
                
                
            }
            else{
                //session.setAttribute("connecte", false);
                session.invalidate();  
                session = null;
                                             
            }
        }
        
        if(action.equals("deconnecter")){
            //session.setAttribute("connecte", false);
            session = null;
            session = request.getSession();
            session.invalidate();

        }

   }
//        RequestDispatcher dp = request.getRequestDispatcher("/ServletFilm?action=index");
//        dp.forward(request, response);
        response.sendRedirect(page);
      }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
